package com.lotus.videotest;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class VideoTestActivity extends Activity {
	
	private File videoFile,dir;
	private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;
	private Button startButton, stopButton;
	private MediaRecorder mRecorder;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mSurfaceView = (SurfaceView) findViewById(R.id.SurfaceView01);       
        mSurfaceHolder = mSurfaceView.getHolder();       
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);     
        startButton=(Button)findViewById(R.id.Button01);    
        stopButton=(Button)findViewById(R.id.Button02);  
        
        File tmpFile = Environment.getExternalStorageDirectory();
        String path = tmpFile.getAbsolutePath() + File.separator + "V" + File.separator;
        dir = new File(path);
        if (!dir.exists()) {
        	dir.mkdir(); 
        } 
         
        mRecorder = new MediaRecorder();
        
        startButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				record();
			}

		});
        
        stopButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mRecorder.stop();
				mRecorder.reset();
				mRecorder.release();
				mRecorder = null;
			}
		});
    }
    
    private void record() {
    	// TODO Auto-generated method stub
    	try {
			videoFile = File.createTempFile("videotest", ".3gp",dir);
			mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
			mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
			mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			mRecorder.setVideoSize(480, 320);
			mRecorder.setVideoFrameRate(15);
			mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
			mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			mRecorder.setMaxDuration(10000);
			mRecorder.setOutputFile(videoFile.getAbsolutePath());
			mRecorder.prepare();
			mRecorder.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}